<?php
	class Category_Model extends CI_Model {
		function __construct()
		{
			parent::__construct();	
		}
		
		function add($input)
		{
                    if($input['name_th'] != null) {
                        $this->load->helper('security');
                        $data = array(
                                'name_th' => $input['name_th'] ,
                                'name_en' => $input['name_en'] ,
                                'parent' => $input['parent']
                        );
                        $this->db->insert('categories', $data);
                        return $this->db->insert_id();
                    }else{
                        return 0;
                    }
		}
		
		function lists($id=-1,$p=1,$limit=50)
                {
			$start = ($p-1)*50;
			$this->db->where('active', 1);
			if($id!=-1){
			    $this->db->where('parent', $id);
			}
			$this->db->order_by("name_th", "ASC");
			$this->db->limit($limit , $start);
			$this->db->from('categories');
			$query = $this->db->get();
			return $query->result();
                }
		
		function update($input)
		{
                    $this->load->helper('security');
                    if($input['id'] != null) {
                        $data = array(
                            'name_th' => $input['name_th'] ,
                            'name_en' => $input['name_en'] ,
                            'parent' => $input['parent']
                        );
                        $this->db->where('id', $input['id']);
                        $this->db->update('categories', $data);
                        return $this->db->affected_rows();
                    }else{
                        return 0;
                    }
		}
		
		function update_img($input)
		{
			if($this->session->userdata('admin_logged_in') == TRUE){
				if($input['id'] != null) {
					$data = array(
						'img' => $input['img'] ,
					);
					$this->db->where('id', $input['id']);
					$this->db->update('categories', $data);
					return $this->db->affected_rows();
				}else{
					return 0;
				}
			}else{
				return 0;
			}
		}
		
		function get_img($input)
		{
			if($input['id'] != null) {
				$this->db->select('img');
				$this->db->where('id', $input['id']);
				$this->db->from('categories');
				$query2 = $this->db->get();
				return $query2->row();
			}else{
				return 0;
			}
		}
		
		function delete($id)
		{
                    if($this->countSubCat($id)<1){
                        $this->load->model('admin/product_model','product_model');
                        if($this->product_model->countProductInCat($id)<1){
                            $this->db->where('id', $id); 
                            return $this->db->delete('categories');
                        }else{
                            return 0;
                        }
                    }else{
                        return 0;
                    }
		}
		
		function getCatNameByCatId($id)
		{
			$query = $this->getCatInfoByCatId($id);
			return $query['name_th'];
		}
		
		function getCatInfoByCatId($id)
		{
			$query = $this->db->get_where('categories', array('id' => $id));
			return $query->row_array();
		}
		
		function countSubCat($id)
		{
                    $query = $this->db->get_where('categories', array('parent' => $id));
                    return $query->num_rows();
		}
		
		function getCatParrentId($id)
		{
			$query = $this->getCatInfoByCatId($id);
			return $query['parent'];
		}
	}


/* End of file category_model.php */
/* Location: ./application/models/admin/category_model.php */